﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Net;
using WomanMotion.WebApi.Api.Utility;
using WomanMotion.WebApi.Infrastructure.Common;
using WomanMotion.WebApi.Infrastructure.DataBaseCommon;
using WomanMotion.WebApi.Infrastructure.UnifyResult;
using WomanMotion.WebApi.Model.Dtos.SendMessage;
using WomanMotion.WebApi.Model.Dtos.Team;
using WomanMotion.WebApi.Service.IService;

namespace WomanMotion.WebApi.Api.Controllers
{/// <summary>
 /// 推送消息管理
 /// </summary>
    [Route("api/[controller]/[action]")]
    [XSSFilter]
    [ApiController]
    [DescriptionName("推送消息管理", true, 40)]
    public class SendMessageController : BaseController
    {
        private readonly ISendMessageService _sendMessageService;
        private readonly IMatchService _matchService;
        private readonly IHttpContextAccessor _httpContextAccessor;
        protected readonly IUsersService _users;
        private readonly IConfiguration _configuration;
        protected readonly IClassifyService _classifyService;
        protected readonly ITeamService _teamService;
        protected readonly IWatchService _watchService;

        public SendMessageController(ILogger<SendMessageController> logger, ISendMessageService sendMessageService, IMatchService matchService, IHttpContextAccessor httpContextAccessor, IUsersService users, IConfiguration configuration, IClassifyService classifyService, ITeamService teamService, IWatchService watchService) : base(logger)
        {
            _sendMessageService = sendMessageService;
            _matchService = matchService;
            _httpContextAccessor = httpContextAccessor;
            _users = users;
            _configuration = configuration;
            _classifyService = classifyService;
            _teamService = teamService;
            _watchService = watchService;
        }

        /// <summary>
        /// 创建订阅消息
        /// </summary>
        /// <param name="dto">DTO</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate<SendMessageVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("创建订阅消息", true, "订阅消息管理", "List", "SendMessageList")]
        public async Task<IResultOutput> Created(CreatedSendMessageDTO dto)
        {
            //await _sendMessageService.SendMsg();
            var match = await _matchService.GetAsync(m => m.Id == dto.Matchid);
            if (match == null)
            {
                return ResultOutput.NotOk("比赛信息不存在");
            }
            var userid = _httpContextAccessor.HttpContext.User.Claims.Where(m => m.Type == "UserId").First().Value;
            await _sendMessageService.SendMsg();
            var isdata = await _sendMessageService.GetAnyAsync(m => m.Userid == long.Parse(userid) && m.Matchid == match.Id);
            if (isdata)
            {
                return ResultOutput.NotOk("此比赛已经订阅,不用重新订阅");
            }
            var user = await _users.GetEntityAsync(m => m.Id == long.Parse(userid));
            if (user.Wxid == null || user.Wxid == "")
            {
                return ResultOutput.NotOk("你没有绑定微信账号订阅失败");
            }
            dto.Userid = long.Parse(userid);
            dto.Matchtime = match.Starttime;
            dto.Issend = 0;
            var res = await _sendMessageService.CreatedAsync(dto);
            if (res != null)
            {
                return ResultOutput.Ok(res, "订阅成功");
            }
            else
            {
                return ResultOutput.NotOk("订阅失败");
            }
        }

        /// <summary>
        /// 分页订阅消息数据
        /// </summary>
        /// <param name="pars">查询条件</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate<PageViewBase<List<SendMessageVM>>>), (int)HttpStatusCode.OK)]
        [DescriptionName("分页订阅消息数据")]
        public async Task<IResultOutput> GetPageList(QueryParameterBase pars)
        {
            var res = await _sendMessageService.GetPageNavListAsync(pars);
            if (res.Data == null || res.Data.Count == 0)
            {
                return ResultOutput.NotOk("没有数据");
            }
            foreach (var item in res.Data)
            {
                var classify = await _classifyService.GetByIdAsync(item.Match.Classid);
                var teama = await _teamService.GetByIdAsync(item.Match.Teamaid);
                var teamb = await _teamService.GetByIdAsync(item.Match.Teambid);
                var watch = await _watchService.GetByIdAsync(item.Match.Watchid);
                item.Match.Classify = classify;
                item.Match.Teama = teama;
                item.Match.Teamb = teamb;
                item.Match.Watch = watch;
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 获取订阅模板ID
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(SwaggerResultTemplate<SendMegTemp>), (int)HttpStatusCode.OK)]
        [DescriptionName("获取订阅模板ID")]
        [AllowAnonymous]
        public IResultOutput GetTemplateId()
        {
            var tokenModel = _configuration.GetSection("SendMegTemp").Get<SendMegTemp>();
            return ResultOutput.Ok(tokenModel, "操作成功");
        }
    }
}